//
//  ZTXYiChangChuLiAlControllerViewController.m
//  NtoccDriverNew
//
//  Created by 张天兴 on 2023/12/7.
//  Copyright © 2023 GCY. All rights reserved.
//

#import "ZTXYiChangChuLiAlControllerViewController.h"
#import "ZTXChuLiHeaderCell.h"
#import "ZTXYiChangChuLiImageCell.h"
#import "ZTXMeRedCell.h"
#import "TXPingTaiRedCell.h"
#import "ZTXRedBottomCell.h"
#import "XMTextView.h"
@interface ZTXYiChangChuLiAlControllerViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, strong) NSArray *titLabelArr;
@property (nonatomic, strong) NSMutableArray *contentArr;
@property (nonatomic, strong) NSString *imgArrStr;
@property (nonatomic, strong) NSString *code;
@property (nonatomic, strong) NSString *orderDetailCode;
@property (nonatomic, strong) NSString *parentCode;
@property (nonatomic, strong) NSString *contentStr;

@end

@implementation ZTXYiChangChuLiAlControllerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = BackgroundColor;

    [self baseUI];
}

- (void)baseUI{
    [self.view addSubview:self.tableView];
    self.dataArray = [@[] mutableCopy];
    self.contentArr = [@[] mutableCopy];
    if ([self.type isEqualToString:@"0"]) {
        self.titLabelArr = @[@"投诉说明",@"发生地址",@"发生时间"];
        [self freightIndexListRequest];
        self.navigationItem.title = @"异常处理";
       
    }else{
        self.titLabelArr = @[@"投诉人",@"发生地点",@"发生时间",@"关联单号",@"投诉原因"];
        [self tousuDetailRequest];
        self.navigationItem.title = @"投诉详情";
    }

}


#pragma mark - LazyLoad
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.frame = CGRectMake(0, 10, ScreenW, ScreenH - NavHeight - 10);
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        //注册单元格
        [self.tableView registerNib:[UINib nibWithNibName:@"ZTXChuLiHeaderCell" bundle:nil] forCellReuseIdentifier:@"ZTXChuLiHeaderCell"];
        [self.tableView registerNib:[UINib nibWithNibName:@"ZTXYiChangChuLiImageCell" bundle:nil] forCellReuseIdentifier:@"ZTXYiChangChuLiImageCell"];
        [self.tableView registerNib:[UINib nibWithNibName:@"TXPingTaiRedCell" bundle:nil] forCellReuseIdentifier:@"TXPingTaiRedCell"];
        [self.tableView registerNib:[UINib nibWithNibName:@"ZTXMeRedCell" bundle:nil] forCellReuseIdentifier:@"ZTXMeRedCell"];
        [self.tableView registerNib:[UINib nibWithNibName:@"ZTXRedBottomCell" bundle:nil] forCellReuseIdentifier:@"ZTXRedBottomCell"];
        
    }
    
    return _tableView;
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 4;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return self.contentArr.count;
    }else if (section == 1){
        return 1;
    }else if (section == 2){
        return self.dataArray.count;
    }else{
        return 1;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        ZTXChuLiHeaderCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ZTXChuLiHeaderCell" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        if(self.contentArr.count != 0){
            cell.titleLabelx.text = self.titLabelArr[indexPath.row];
            cell.contentLabel.text = self.contentArr[indexPath.row];
        }
        return cell;
    }else if (indexPath.section == 1){
        ZTXYiChangChuLiImageCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ZTXYiChangChuLiImageCell" forIndexPath:indexPath];
        if ([self.imgArrStr isNotBlank]) {
            [cell fuzhiImagArr:self.imgArrStr];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else  if (indexPath.section == 2){
        NSDictionary *dic = self.dataArray[indexPath.row];
        if ([CJStringWithFormat(@"%@",dic[@"partyType"]) isEqualToString:@"PLAT_ADMIN"]) {
            TXPingTaiRedCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"TXPingTaiRedCell" forIndexPath:indexPath];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell fuzhiDic:dic];
            return cell;
        }else{
            ZTXMeRedCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ZTXMeRedCell" forIndexPath:indexPath];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            [cell fuzhiZTXMeRedCell:dic];
            return cell;
        }
    }else{
        ZTXRedBottomCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"ZTXRedBottomCell" forIndexPath:indexPath];
        [cell.huifuButton addTarget:self action:@selector(huifuButtonAction:) forControlEvents:(UIControlEventTouchUpInside)];
        cell.txtView.textViewListening = ^(NSString *textViewStr) {
            self.contentStr = textViewStr;
        };
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
    
   
}

#pragma mark - UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.section == 0){
        return [ZTXChuLiHeaderCell heightCentent:self.contentArr[indexPath.row]];
    }else if (indexPath.section == 1){
        if ([self.imgArrStr isNotBlank]) {
            return [ZTXYiChangChuLiImageCell cententImagHeight:self.imgArrStr];
        }else{
            return 0;
        }
    }else if (indexPath.section == 2){
        NSDictionary *dic = self.dataArray[indexPath.row];
        if ([CJStringWithFormat(@"%@",dic[@"partyType"]) isEqualToString:@"PLAT_ADMIN"]) {
            return [TXPingTaiRedCell fuzhiDicHeight:dic];
        }else{
            return [ZTXMeRedCell fuzhiDicHeight:dic];
        }
    }else if (indexPath.section == 3){
        return 200;
    }
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001f;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
- (void)huifuButtonAction:(UIButton *)btn{
    CGPoint point = btn.center;//找到btn中心点记录下来
    point = [self.tableView convertPoint:point fromView:btn.superview];
    NSIndexPath* indexpath = [self.tableView indexPathForRowAtPoint:point];
    ZTXRedBottomCell *cell = [self.tableView cellForRowAtIndexPath:indexpath];
    if ([self.type isEqualToString:@"0"]) {//异常上报
        [self huifuYiChang:cell];
    }else{//投诉
        [self huifuTouSu:cell];
    }
  
}
- (void)huifuYiChang:(ZTXRedBottomCell *)cell{
    if (self.contentStr.length == 0) {
        [XSInfoView showInfo:@"请填输入回复内容" onView:self.view];
        return;
    }
    //异常上报
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:self.code forKey:@"code"];
    [params setValue:self.orderDetailCode forKey:@"orderDetailCode"];
    [params setValue:self.code forKey:@"parentCode"];
    [params setValue:self.contentStr forKey:@"handleIdea"];

    NSString *urlString = CJStringWithFormat(@"%@%@", [NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl], TRANSPORT_ABNORMAL_HANDLE);
    
    [HTTPManager postNetworkRequest: urlString withParameter:params withVC:self netName:self.navigationItem.title success:^(id response, NSString *msg) {
        cell.txtView.text = @"";
        [self freightIndexListRequest];
        NSLog(@"%@",response);
    } failure:^(NSInteger code, NSString *msg) {
        
    }];
}
- (void)huifuTouSu:(ZTXRedBottomCell *)cell{
    if (self.contentStr.length == 0) {
        [XSInfoView showInfo:@"请填输入回复内容" onView:self.view];
        return;
    }
    //异常上报
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    [params setValue:self.code forKey:@"code"];
    [params setValue:self.orderDetailCode forKey:@"orderDetailCode"];
    [params setValue:self.code forKey:@"parentCode"];
    [params setValue:self.contentStr forKey:@"handleIdea"];

    NSString *urlString = CJStringWithFormat(@"%@%@", [NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl], ORDER_DETAIL_ABNORMAL_HANDLE);
    
    [HTTPManager postNetworkRequest: urlString withParameter:params withVC:self netName:self.navigationItem.title success:^(id response, NSString *msg) {
        cell.txtView.text = @"";
        [self tousuDetailRequest];
        NSLog(@"%@",response);
    } failure:^(NSInteger code, NSString *msg) {
        
    }];
}
- (void)freightIndexListRequest {
    [self.contentArr removeAllObjects];
    [self.dataArray removeAllObjects];
     NSString *urlString = CJStringWithFormat(@"%@%@/%@", [NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl], TRANSPORT_ABNORMAL,self.orderCode);
    [HTTPManager getNetworkRequest:urlString withParameter:@{} withVC:self netName:@"异常处理反馈信息" success:^(id response, NSString *msg) {
        [self.contentArr addObject:response[@"description"]];
        [self.contentArr addObject:response[@"address"]];
        [self.contentArr addObject:response[@"gmtModified"]];
        self.imgArrStr = response[@"sceneImg"];
        [self.dataArray addObjectsFromArray:response[@"childRenVoList"]];
        self.code = response[@"code"];
        self.orderDetailCode = response[@"orderDetailCode"];
        self.parentCode = response[@"parentCode"];
        [self.tableView reloadData];
        [self.tableView scrollToRow:0 inSection:3 atScrollPosition:(UITableViewScrollPositionBottom) animated:NO];
     } failure:^(NSInteger code, NSString *msg) {
         if(code == 500){
             [self.navigationController popViewControllerAnimated:YES];
         }
     }];
}
- (void)tousuDetailRequest {
    [self.contentArr removeAllObjects];
    [self.dataArray removeAllObjects];
    WEAKSELF
    [HTTPManager getNetworkRequest:CJStringWithFormat(@"%@%@/%@", [NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl], ORDER_DETAIL_ABNORMAL ,self.orderCode) withParameter:@{@"token":[UserDefaultUtil valueForKey:USER_TOKEN],} withVC:self netName:@"投诉信息详情" success:^(id response, NSString *msg) {
        if (_abnormalFrom == 0) {
            //投诉我的
            [weakSelf.contentArr addObject:[NSString stringWithFormat:@"%@（%@）", [NSString nullToString:CJStringWithFormat(@"%@", response[@"shipmentName"])], [NSString nullToString:CJStringWithFormat(@"%@", response[@"shipmentPhone"])]]];
        } else {
            //我投诉的
            [weakSelf.contentArr addObject:[NSString stringWithFormat:@"%@（%@）", [NSString nullToString:CJStringWithFormat(@"%@", response[@"driverName"])], [NSString nullToString:CJStringWithFormat(@"%@", response[@"driverPhone"])]]];
        }

        [weakSelf.contentArr addObject:[NSString nullToString:response[@"address"]]];
        [weakSelf.contentArr addObject:[NSString nullToString:response[@"occurrenceTime"]]];
        [weakSelf.contentArr addObject:[NSString nullToString:response[@"orderCode"]]];
        [weakSelf.contentArr addObject:[NSString nullToString:response[@"abnormalRemark"]]];
        weakSelf.imgArrStr = response[@"abnormalImage"];
        [self.dataArray addObjectsFromArray:response[@"childRenVoList"]];
        self.code = response[@"code"];
        self.orderDetailCode = response[@"orderDetailCode"];
        self.parentCode = response[@"parentCode"];
        [self.tableView reloadData];
        [self.tableView scrollToRow:0 inSection:3 atScrollPosition:(UITableViewScrollPositionBottom) animated:NO];
        
    } failure:^(NSInteger code, NSString *msg) {
        
    }];
}
@end
